﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class BlogCategory : PageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
            PageID = Website.BlogTemplatePageID.Value;
            checkTitleForRedirect = false;
            base.Page_PreInit(sender, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
                return;

            if (Page.RouteData.Values["BlogCategoryID"] != null)
            {

                int blogCategoryID = Convert.ToInt32(Page.RouteData.Values["BlogCategoryID"].ToString());

                DataSet categoryDS = WebsiteData.BlogCategoryDetails(blogCategoryID);
                BlogCategoryProperties category = BlogCategoryProperties.Create(categoryDS);

                //do 301 redirect if page title is incorrect
                if (RouteData.Values["Title"] != null)
                    if (RouteData.Values["Title"].ToString() != StringUtils.CleanStringForURL(category.BlogCategoryTitle).ToLower())
                        Response.RedirectPermanent(BlogCategoryProperties.GetURL(this, category), true);

                TitleLiteral.Text = category.BlogCategoryTitle;

                BlogPostRepeater.DataSource = WebsiteData.BlogPostListByCategory(blogCategoryID);
                BlogPostRepeater.DataBind();

            }
            else
            {

                TitleLiteral.Text = "Latest";

                BlogPostRepeater.DataSource = WebsiteData.BlogPostListTop(10);
                BlogPostRepeater.DataBind();


            }

        }

        protected void BlogPostRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
        {

            DataRowView drv = (DataRowView)e.Item.DataItem;
            BlogPostProperties post = BlogPostProperties.Create(drv.Row);

            HyperLink TitleHyperLink = (HyperLink)e.Item.FindControl("TitleHyperLink");
            Literal AuthorLiteral = (Literal)e.Item.FindControl("AuthorLiteral");
            Literal DateLiteral = (Literal)e.Item.FindControl("DateLiteral");
            Literal SummaryLiteral = (Literal)e.Item.FindControl("SummaryLiteral");
            HyperLink ReadMoreHyperLink = (HyperLink)e.Item.FindControl("ReadMoreHyperLink");
            Image BlogImage = (Image)e.Item.FindControl("BlogImage");

            TitleHyperLink.Text = post.BlogPostTitle;
            TitleHyperLink.NavigateUrl = BlogPostProperties.GetURL(this, post);

            ReadMoreHyperLink.NavigateUrl = TitleHyperLink.NavigateUrl;

            AuthorLiteral.Text = post.BlogPostAuthor;
            DateLiteral.Text = post.BlogPostCreateDate.Value.ToString("D");
            SummaryLiteral.Text = post.BlogPostSummary;
            BlogImage.ImageUrl = "/SiteImagesCache/large-" + post.BlogPostImage;

        }

    }
}